package com.css.zfzx.sjcj.modules.hddcwyLava.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.modules.hddcwyLava.repository.HddcWyLavaNativeRepository;
import com.css.zfzx.sjcj.modules.hddcwyLava.repository.HddcWyLavaRepository;
import com.css.zfzx.sjcj.modules.hddcwyLava.repository.entity.HddcWyLavaEntity;
import com.css.zfzx.sjcj.modules.hddcwyLava.service.HddcWyLavaService;
import com.css.zfzx.sjcj.modules.hddcwyLava.viewobjects.HddcWyLavaQueryParams;
import com.css.zfzx.sjcj.modules.hddcwyLava.viewobjects.HddcWyLavaVO;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhangcong
 * @date 2020-12-02
 */
@Service
public class HddcWyLavaServiceImpl implements HddcWyLavaService {

	@Autowired
    private HddcWyLavaRepository hddcWyLavaRepository;
    @Autowired
    private HddcWyLavaNativeRepository hddcWyLavaNativeRepository;

    @Override
    public JSONObject queryHddcWyLavas(HddcWyLavaQueryParams queryParams , int curPage, int pageSize) {
        Page<HddcWyLavaEntity> hddcWyLavaPage = this.hddcWyLavaNativeRepository.queryHddcWyLavas(queryParams ,
                curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcWyLavaPage);
        return jsonObject;
    }


    @Override
    public HddcWyLavaEntity getHddcWyLava(String id) {
        HddcWyLavaEntity hddcWyLava = this.hddcWyLavaRepository.findById(id).orElse(null);
         return hddcWyLava;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyLavaEntity saveHddcWyLava(HddcWyLavaEntity hddcWyLava) {
        String uuid = UUIDGenerator.getUUID();
        hddcWyLava.setId(uuid);
        hddcWyLava.setCreateUser(PlatformSessionUtils.getUserId());
        hddcWyLava.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyLavaRepository.save(hddcWyLava);
        return hddcWyLava;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyLavaEntity updateHddcWyLava(HddcWyLavaEntity hddcWyLava) {
        hddcWyLava.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcWyLava.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyLavaRepository.save(hddcWyLava);
        return hddcWyLava;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcWyLavas(List<String> ids) {
        List<HddcWyLavaEntity> hddcWyLavaList = this.hddcWyLavaRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(hddcWyLavaList) && hddcWyLavaList.size() > 0) {
            for(HddcWyLavaEntity hddcWyLava : hddcWyLavaList) {
                this.hddcWyLavaRepository.delete(hddcWyLava);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

}
